/*
 * Copyright 2020-2021 redragon.dongbin
 *
 * This file is part of redragon-erp/赤龙ERP.

 * redragon-erp/赤龙ERP is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.

 * redragon-erp/赤龙ERP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with redragon-erp/赤龙ERP.  If not, see <https://www.gnu.org/licenses/>.
 */
package com.erp.prod.workcenter.dao.hibernate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.redragon.framework.annotation.Permissions;
import org.redragon.framework.annotation.Permissions.PermissionType;
import org.redragon.framework.annotation.SqlParam;
import org.redragon.framework.hibernate.DaoSupport;
import org.redragon.framework.hibernate.model.Pages;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.erp.prod.workcenter.dao.ProdWorkCenterResourceDao;
import com.erp.prod.workcenter.dao.model.ProdWorkCenterResource;
import com.erp.prod.workcenter.dao.model.ProdWorkCenterResourceCO;

@Repository
public class ProdWorkCenterResourceDaoImpl implements ProdWorkCenterResourceDao{ 

    //注入DaoSupport工具类
    @Autowired
    private DaoSupport daoSupport;
    
    @Override
    public void insertDataObject(ProdWorkCenterResource obj) {
        this.daoSupport.insertDataTransaction(obj);
    }

    @Override
    public void updateDataObject(ProdWorkCenterResource obj) {
        this.daoSupport.updateDataTransaction(obj);
    }
    
    @Override
    public void insertOrUpdateDataObject(ProdWorkCenterResource obj) {
        this.daoSupport.insertOrUpdateDataTransaction(obj);
    }

    @Override
    public void deleteDataObject(ProdWorkCenterResource obj) {
        this.daoSupport.deleteDataTransactionJPA(obj);
    }

    @Override
    public List<ProdWorkCenterResource> getDataObjects() {
        return this.daoSupport.getDataAllObject(ProdWorkCenterResource.class);
    }

    @Override
    public ProdWorkCenterResource getDataObject(int id) {
        return (ProdWorkCenterResource)this.daoSupport.getDataObject(ProdWorkCenterResource.class, id);
    }
    
    @Override
    public ProdWorkCenterResource getDataObject(String code) {
        return null;
    }
    
    @Override
    public List<ProdWorkCenterResource> getDataObjects(ProdWorkCenterResourceCO paramObj) {
        String sql = "select c.* from prod_work_center_resource c where work_center_code=:code";
        
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("code", paramObj.getWorkCenterCode());
        
        sql = sql + " order by c.work_center_resource_id desc";
        
        Map<String, Class<?>> entity = new HashMap<String, Class<?>>();
        entity.put("c", ProdWorkCenterResource.class);
        
        return this.daoSupport.selectDataSql(sql, entity, args);
    }
    
    @Override
    public List<ProdWorkCenterResource> getDataObjects(Pages pages) {
        return null;
    }
    
    @Override
    public List<ProdWorkCenterResource> getDataObjects(Pages pages, ProdWorkCenterResourceCO paramObj) {
        return null;
    }

    @Override
    public List<Map<String, Object>> getDataObjectsArray(Pages pages, ProdWorkCenterResourceCO paramObj) {
        return null;
    }
    
    @Override
    @Permissions(PermissionType.DATA_AUTH)
    public List<ProdWorkCenterResource> getDataObjectsForDataAuth(@SqlParam String dataAuthSQL, Pages pages, ProdWorkCenterResourceCO paramObj) {
        return null;
    }
    
    @Override
    public int getResourceCountByWorkCenterCode(String workCenterCode) {
        String sql = "select count(1) from prod_work_center_resource c where c.work_center_code = :code";
        
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("code", workCenterCode);
        
        List list = this.daoSupport.selectDataSqlCount(sql, args);
        if(list.size()>0) {
            return Integer.parseInt(String.valueOf(list.get(0)));
        }
        
        return 0;
    }
    
    @Override
    public List<ProdWorkCenterResource> getResourceListByWorkCenterCode(Pages pages, ProdWorkCenterResourceCO paramObj) {
        String sql = "select c.* from prod_work_center_resource c where work_center_code=:code";
        
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("code", paramObj.getWorkCenterCode());
        
        sql = sql + " order by c.work_center_resource_id desc";
        
        Map<String, Class<?>> entity = new HashMap<String, Class<?>>();
        entity.put("c", ProdWorkCenterResource.class);
        
        return this.daoSupport.getDataSqlByPage(sql, entity, args, pages);
    }
    
    @Override
    public void updateLineForVersion(String code) {
        String sql = "update prod_work_center_resource set version = version+1 where work_center_code = :code";
        
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("code", code);
        
        this.daoSupport.executeSQLTransaction(sql, args);
    }
    
    @Override
    public ProdWorkCenterResource getProdWorkCenterResource(String workCenterCode, String resourceCode) {
        String sql = "select r.* from prod_work_center_resource r where r.work_center_code = :wcode and r.resource_code = :rcode";
        
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("wcode", workCenterCode);
        args.put("rcode", resourceCode);
        
        Map<String, Class<?>> entity = new HashMap<String, Class<?>>();
        entity.put("r", ProdWorkCenterResource.class);
        
        List<ProdWorkCenterResource> list = this.daoSupport.selectDataSql(sql, entity, args);
        if(list!=null&&list.size()>0) {
            return list.get(0);
        }
        
        return null;
    }
    
}
